let opt;
let temp = {
  toname: '',
  "best-wishes": '',
  nickName: ''
}

const saveDataToApp = function (that, e) {
  let userInfo = that.data.userInfo;
  let data = e.detail.value;
  userInfo.nickName = data.nickName;
  app.setUserInfo(userInfo);
  app.setWishes(data['best-wishes']);
  app.setToName(data.toname)
  app.globalData.istwoOnload = "1"
}

const saveDataToAppTemp = function (that, data) {
  app.setTempNickName(data.nickName);
  app.setToName(data.toname, true)
  app.setWishes(data['best-wishes'], true);
}

var app = getApp()
Page({
  data: {
    isRed: false,
    tempFilePaths: '',
    isVip: ''
  },
  "more-template": function (e) {
    let that = this;

    // HACK
    // 防止触发more-template时,setToName,setNickName,setWishes这三个onblur事件还没有完成
    // 最好使用promise;
    setTimeout(function () {
      //防止有几条数据没有改的情况发生
      temp.toname = temp.toname || that.data.toname;
      temp['best-wishes'] = temp['best-wishes'] || that.data.wishes;
      temp.nickName = temp.nickName || that.data.userInfo.nickName;

      saveDataToAppTemp(that, temp);
      wx.navigateTo({
        url: `/pages/more/more`
      })
    }, 50)
    /*this.setData({showMore:!this.data['showMore']})*/
  },
  save: function (e) {
    var that = this
    var temp = e.detail.value["best-wishes"]
    var toname = e.detail.value["toname"]
    var nickName = e.detail.value["nickName"]
    console.log("temp" + temp + "toname" + toname + "nickName" + nickName)
    wx.request({
      url: 'https://wsh.weyifeng.com/sensitiveWord',
      data: {
        sensitive: temp,
        toname: toname,
        nickName: nickName
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.size > 0) {
          wx.showModal({
            title: '提示',
            content: '不能包含敏感字符',
            success: function (res) { },
          })
        } else {
          saveDataToApp(that, e);
          app.clearWishes(true);
          app.clearTempNickName();
          app.clearTempToName();
          app.settempFilePaths(that.data.tempFilePaths)
          wx.navigateBack();
        }
      }
    })
  },
  cancel: function (e) {
    app.clearWishes(true);
    app.clearTempNickName();
    app.clearTempToName();
    wx.navigateBack();
  },
  setToName: function (e) {
    temp['toname'] = e.detail.value;
    app.setToName(e.detail.value, true)
  },
  setNickName: function (e) {
    temp['nickName'] = e.detail.value;
    app.setNickName(e.detail.value, true)
  },
  setWishes: function (e) {
    temp['best-wishes'] = e.detail.value;
    app.setWishes(e.detail.value, true);
  },
  bindChangeText: function (e) {
    let txt = e.detail.value
    if (txt.length > 90) {
      this.setData({
        'isRed': true
      })
    } else {
      this.setData({
        'isRed': false
      })
    }
  },
  onShow: function () {
    let that = this;
    app.getUserInfos(function (userInfo) {
      app.userInfo
      let _userInfo = Object.assign({}, userInfo);
      _userInfo.nickName = app.getTempNickName() || _userInfo.nickName;
      that.setData({
        userInfo: _userInfo,
        isVip: app.globalData.isVip
      })
    })

    this.setData({
      toname: app.getToName(true) || app.getToName(),
      wishes: app.getWishes(true) || app.getWishes(),
    });
    if (app.globalData.tempFilePaths != '') {
      this.setData({
        tempFilePaths: app.globalData.tempFilePaths
      })
    }
  },
  onLoad: function (options) {
    var that = this;
    opt = options;
  },
  vipbut: function () {
    wx.navigateTo({
      url: '/pages/vippage/vippage',
    })
  }, upFileimg: function () {
    var _this = this;
    if (app.globalData.isVip == '1') {
      wx.chooseImage({
        success: function (res) {
          var tempFilePaths = res.tempFilePaths
          app.globalData.tempFilePaths = res.tempFilePaths[0]
          _this.setData({
            tempFilePaths: res.tempFilePaths[0]
          })
          console.log(res.tempFilePaths)
          wx.uploadFile({
            url: 'https://wsh.weyifeng.com/getuploadimg',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'typeid': '1',
              'userOpenid': app.globalData.openid,
              'userName': app.globalData.userInfo.nickName
            },
            header: {
              'Content-Type': 'multipart/form-data'
            },
            success: function (res) {
              console.log("res:" + res.data)
              var json = JSON.parse(res.data)
              var data = json.success
              var msg = json.msg
              if (data) {
                app.globalData.tempFilePathsID = msg
              }
            }
          })
        }
      })
    }
    else {
      wx.showModal({
        content: '支付1元，可以使用本程序所有功能，是否支付？',
        cancelText: '否',
        confirmText: '是',
        success: function (res) {
          if (res.confirm) {
            app.vipPay()
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
          console.log('提示成为会员!')
        }
      })
    }
  }
  /* wx.chooseImage({
     count: 1, // 默认9  
     sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
     sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
     success: function (res) {
       // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
       _this.setData({
         tempFilePaths: res.tempFilePaths
       })
       console.log(res)
     }
   }) 
 }*/
  , getbg: function () {
    wx.navigateTo({
      url: '/pages/bgImages/bgImages',
    })
  }, getVip: function () {
    app.vipPay()
    this.onLoad()
  }
})
